<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="分类名称">
              <el-input v-model="query.name" placeholder="输入分类名称" clearable> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <topOpts isJudgePer addText="新建分类" @add="addOrUpdateHandle()" />
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list" @sort-change='sortChange' :customColumn="true">
          <el-table-column prop="name" label="分类名称" align="left" show-overflow-tooltip />
          <el-table-column prop="sortcode" label="排序" align="left" />
          <el-table-column prop="icon" label="图标" align="left">
            <template slot-scope="scope">
              <el-image v-if="scope.row.url" :src="define.comUrl+scope.row.url" class="el-upload-list__item-thumbnail"></el-image>
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否启用" prop="showmark">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.showmark" active-value="1" inactive-value="0" @change="showmarkChange($event, scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" align="left" show-overflow-tooltip />
          <el-table-column prop="creatorusername" label="创建人" align="left" show-overflow-tooltip />
          <el-table-column prop="creatortime" label="创建时间" min-width="140" align="left" />
          <el-table-column label="操作" fixed="right" min-width="90">
            <template slot-scope="scope">
              <tableOpts isJudgePer @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)" />
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
          @pagination="initData" />
      </div>
    </div>
    <JNPF-Form v-if="formVisible" ref="JNPFForm" @refreshSupplyCategory="refreshSupplyCategory" />
  </div>
</template>

<script>
  import JNPFForm from './Form'
  import { getAssoSupplyTypeList, delAssoSupplyType, updateAssoSupplyType } from '@/api/asso/supply/category'

  export default {
    name: 'assosupplycategoryindex',
    components: {
      JNPFForm
    },
    data() {
      return {
        columnOptions: [],
        query: {
          name: undefined,
        },
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          superQueryJson: '',
          currentPage: 1,
          pageSize: 20,
          sort: "desc",
          sidx: "",
        },
        formVisible: false,
      }
    },
    created() {
      this.initData()
    },
    methods: {
      sortChange({
        column,
        prop,
        order
      }) {
        this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
        this.listQuery.sidx = !order ? '' : prop
        this.initData()
      },
      initData() {
        this.listLoading = true;
        let _query = {
          ...this.listQuery,
          ...this.query,
          keyword: this.keyword,
          dataType: 0 // 控制是否分页
        };
        getAssoSupplyTypeList(_query).then((res) => {
          res.data.list.forEach(item=>{
            if(JSON.parse(item.icon)[0]){
              item.url = JSON.parse(item.icon)[0].url
            }
          })
          this.list = JSON.parse(JSON.stringify(res.data.list))
          this.total = res.data.pagination.total
          this.listLoading = false
        }).catch(()=>{
          this.listLoading = false
        })
      },
      handleDel(id) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          type: 'warning'
        }).then(() => {
          delAssoSupplyType(id).then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        }).catch(() => {});
      },
      addOrUpdateHandle(id) {
        this.formVisible = true
        this.$nextTick(() => {
          this.$refs.JNPFForm.init(id)
        })
      },
      search() {
        this.listQuery.currentPage = 1
        this.listQuery.pageSize = 20
        this.listQuery.sort = "desc"
        this.listQuery.sidx = ""
        this.initData()
      },
      refreshSupplyCategory(isrRefresh) {
        this.formVisible = false
        if (isrRefresh) this.reset()
      },
      reset() {
        for (let key in this.query) {
          this.query[key] = undefined
        }
        this.search()
      },
      showmarkChange(e, row) {
        updateAssoSupplyType(row.id, row).then((res) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.initData()
            }
          })
        })
      }
    }
  }
</script>
